const implAddress = getApp().globalData.implAddress;
Page({
  data: {
    /* 
      订单状态：
        0，排队中
        1，派单中
        2，已完成
    */
    orders: [{
      order_id: 1,
      status: '排队中',
      order_account: 100,
    }, ],
    allOrder: []
  },

  onShow(options) {
    const that = this
    //获取订单
    wx.request({
      url: implAddress + 'order/' + 1, //1为用户ID，之后实装登录之后可以换成userId
      method: 'GET',
      fail: function (err) {
        console.error(err)
      },
      success: function (res) {
        console.log('所有订单信息', res.data)
        let resOrder = res.data.data
        resOrder.forEach(function (item, index) {
          switch (item.status) {
            case "0":
              resOrder[index].status = '排队中'
              break;
            case "1":
              resOrder[index].status = '派单中'
              break;
            case "2":
              resOrder[index].status = '已完成'
              break;
            default:
              resOrder[index].status = '锟斤拷'
              break
          }
        })
        //处理后更改data数据
        that.setData({
          orders: resOrder,
          allOrder: resOrder
        })
      }
    })
  },

  //搜索
  search: function (e) {
    const that = this;
    setTimeout(function () {
      //开始搜索
      let goodArray = that.data.allOrder
      let result = goodArray.find(goodArray => goodArray.order_id === parseInt(e.detail.value))
      //输出搜索结果
      let resultArray = []
      if (result) {
        console.log('搜索成功！')
        resultArray.push(result)
        that.setData({
          orders: resultArray
        })
      } else {
        console.log('没有匹配的商品')
        //还原显示列表
        that.setData({
          orders: that.data.allOrder
        })
      }
    }, 2000)
  },

  //跳转到对应订单页面
  navTo: function (event) {
    var orderId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/singleOrder/singleOrder?orderId=' + orderId,
      fail: function () {
        wx.showToast({
          title: '跳转失败！',
          icon: 'error'
        })
      }
    })
  }
})